/*
 * 新建工程窗口
 */

DC.front.win.NewProjectWin = function(params, config) {
	var pw = this;
	
	var new_project_form_panel = new DC.front.win.NewProjectFormPanel();
	
	DC.front.win.NewProjectWin.superclass.constructor.call(this, Ext.apply({
    	id : 'new_project_win',
    	title : 'New Project Wizard',
    	width : 430,
    	height : 180,
        border : true,
        bodyBorder : true,
        layout : 'fit',
        modal : true,
        closeAction : 'close',
        items :[ new_project_form_panel ],
        buttons : [{
			text : 'Create',
			handler :function(){
				new_project_form_panel.createProject();
			} 
		}, {
			text : 'Cancle',
			handler :function(){
				pw.close();
			} 
		}]
    }, config ||{}));
};

Ext.extend(DC.front.win.NewProjectWin, Ext.Window, {
	
});

DC.front.win.NewProjectFormPanel = function(params, config) {
	 DC.front.win.NewProjectFormPanel.superclass.constructor.call(this, Ext.apply({
            id : 'new_project_formPanel',
            border : true,
			bodyStyle : 'padding:10px 10px;',
            labelAlign : 'top',
            defaultType : 'textfield',
            defaults : {
                width : 390,
                labelStyle : 'color:#666666; font-size:12px; text-shadow:1px 0 1px #cccccc;',
                labelSeparator : '',
                msgTarget : 'qtip'
            },
            items : [{
            	xtype : 'combo',
            	fieldLabel : 'Programming language',
            	name : 'pro_language',
		    	hiddenName : 'cProject.projectType', // 提交到后台的input的name，重要
		    	editable : false,
		    	triggerAction : 'all', // 显示所有下列数据，一定要设置属性triggerAction为all
		        allowBlank : false, //不允许为空
		    	store : new Ext.data.SimpleStore({
		    		fields : ['text', 'value'],
		    		data : [
						['Java', 'java']
					]
		    	}),
		    	displayField : 'text',
		    	valueField : 'value',
		    	mode : 'local',
		    	emptyText : 'Please Select...',
		    	allowBlank : false,
            	blankText : 'Please select the project type.'
            }, {
            	id : 'project_name',
            	fieldLabel : 'Project name',
            	name : 'cProject.projectName',
            	allowBlank : false,
            	blankText : 'The project name cannot be empty.',
            	maxLength : 255,
                maxLengthText : 'The length of the project name cannot be more than 255.',
                regex: /^[^\/\\<>\*\?\:"\|\.]*[^\/\\<>\*\?\:"\|\.]$/,
                regexText: 'The project name cannot contain "/\<>*?:"|."'
            }]
    }, config ||{}));
};

Ext.extend(DC.front.win.NewProjectFormPanel, Ext.form.FormPanel, {
	 // 添加用户的响应事件
	createProject : function() {
    	
		this.getForm().submit({
            clientValidation : true,
            waitTitle : 'Wait for...',
            waitMsg : 'Creating......',
            method : 'post',
            url : DC.url.CREATE_PROJECT,
            success : function(form, action){
                console.log(action.result);
            	Ext.getCmp("new_project_win").close();
            	var tree = Ext.getCmp("explorer_panel");
            	tree.getRootNode().reload();//重新加载树结点.
			   /* var treeState = Ext.state.Manager.get("treestate");
			    if (treeState) {
			    	tree.expandPath(treeState);
			    }*/
            	/*Ext.getCmp("explorer_panel").getRootNode().appendChild(new Ext.tree.TreeNode({
            		text : nodeName,
            		leaf : false
            	}));*/
            },
            failure : function(form, action){
            	var res = action.result;
                switch (action.failureType) {
	                case Ext.form.Action.CLIENT_INVALID: // 当客户端验证失败时执行
	                     break;
	                case Ext.form.Action.CONNECT_FAILURE:
	                	 DC.util.CommonAction.showMessage('Network Prompt Win', 'Network connection failure, so fail to commit.', Ext.MessageBox.ERROR);
	                     break;
	                case Ext.form.Action.SERVER_INVALID: // 当服务器端的success属性的值返回false时执行
	                    if (res.type == 'WARNING') {
	                    	DC.util.CommonAction.showMessage('Verification Win', res.msg, Ext.MessageBox.WARNING);
	                    } else {
	                    	DC.util.CommonAction.showMessage('Application Prompt Win', res.msg, Ext.MessageBox.ERROR);
	                    }
	            }
            }
        });
    }
});